local Lang = require("common.Lang")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local BagLogic = require("bag.BagLogic")
local EquipLogic = require("bag.EquipLogic")
local ItemDefine = require("bag.ItemDefine")
local GemConfig = require("excel.Gem").gem
local RefineHandle = require("refine.HandlerL")
local TaskCallbacks = require("task.TaskCallbacks")
local TaskDefine = require("task.Define")
local EquipExcel = require("excel.Equip")
local Grid = require("bag.Grid")

function Xiangqiang(human, index, gemPos, gemIndex)
  local equip = human.db.equip[index].data.equip
  if equip.quality <= 1 then
    return
  end
  if equip.gems[gemIndex] ~= 0 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_XIANQIANG_HAVE_GEM)    
  end
  if gemIndex > equip.hole - 1 then
    return
  end
  local grid = human.db.bag[gemPos]
  if not grid or grid.id == 0 or not ItemDefine.isGem(grid.id) then
    return
  end
  if (gemIndex >= 3 and GemConfig[grid.id].hole <= 4) or (gemIndex < 3 and GemConfig[grid.id].hole > 4 )then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_XIANQIANG_CANNOT_XQ_THIS_GEM)
  end
  for i=0, equip.hole - 1 do
    if equip.gems[i] ~= 0 and GemConfig[equip.gems[i]].hole == GemConfig[grid.id].hole then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_XIANQIANG_TYPE_CANNOT_EQUAL)
    end
  end
  
  equip.gems[gemIndex] = grid.id
  BagLogic.delItemByIndex(human, gemPos, 1, "xiangqiang")

  EquipLogic.GC_EQUIP_CHANGE(human,index)
  ObjHuman.doCalc(human)
  RefineHandle.CG_OPEN_REFINE_WINDOW(human)
  TaskCallbacks.taskRefineCB(human, TaskDefine.TASK_REFINE_XQ, EquipExcel.equip[human.db.equip[index].id].pos)  
end

function putOff(human, index, gemIndex)
  local equip = human.db.equip[index].data.equip
  if equip.quality <= 1 or gemIndex >= 5 then
    return 
  end
  if not equip.gems[gemIndex] == 0  then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_NOT_GEM)
  end
  if not BagLogic.checkCanAddItem(human, equip.gems[gemIndex], 1, 1) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.REFINE_BAG_GRID_NOT_ENOUGH)
  end
  BagLogic.addItem(human, equip.gems[gemIndex], 1, 1, "xiangqiang")

  equip.gems[gemIndex] = 0
  EquipLogic.GC_EQUIP_CHANGE(human,index)
  RefineHandle.CG_OPEN_REFINE_WINDOW(human)
  ObjHuman.doCalc(human)  
end

function checkCanPutOn(human, equip, id)
  if equip.quality <= 1 then
    return false
  end
  local isAllXq = true
  for i=0, equip.hole - 1 do
    if equip.gems[i] ~= 0 and GemConfig[equip.gems[i]].hole == GemConfig[id].hole then
      return false
    end
    if (i >= 3 and GemConfig[id].hole <= 4) or (i < 3 and GemConfig[id].hole > 4 )then
      return false
    end
    if equip.gems[i] == 0 then
      isAllXq = false
    end
  end  
  return not isAllXq
end

function checkShowTips(human, info)
  if human.db.lv < 36 then
    return 0
  end
  local item = {}
  for i=0, human.db.bagLen - 1 do
    if ItemDefine.isGem(human.db.bag[i].id) then
      item[#item + 1] = human.db.bag[i].id
    end
  end
  if #item == 0 then
    return 0
  end
  local cache = {}
  for i=0,human.db.equipLen - 1 do
    local grid = human.db.equip[i]
    if grid ~= nil and grid.id ~= 0 then
      local equip = human.db.equip[i].data.equip
      for kk=0, equip.hole - 1 do
        for jj=1, #item do
          if checkCanPutOn(human, equip, item[jj]) then
            if info then
              info.equip[0] = 1
              Grid.create(cache, grid.id, 1)
              Grid.makeItem(cache, info.equip[1], 0, human)
            end          
            return 1
          end
        end
      end      
    end
  end
  return 0
end